package com.pomelo.tiku.activities

import android.content.Intent
import android.os.Bundle
import android.os.Process
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.pomelo.tiku.R
import com.pomelo.tiku.beans.LoginBean
import com.pomelo.tiku.constants.SharePreferenceConstants
import com.pomelo.tiku.constants.UrlConstants
import com.pomelo.tiku.impls.LoginModel
import com.pomelo.tiku.impls.LoginPresenter
import com.pomelo.tiku.interfaces.Contract
import com.pomelo.tiku.managers.SharedPreferenceManager
import com.pomelo.tiku.utils.ClickUtils
import com.pomelo.tiku.views.CommonToast
import kotlinx.android.synthetic.main.activity_login.*
import java.lang.ref.WeakReference

class LoginActivity : BaseActivity(), View.OnClickListener,Contract.ILoginView {
    private var iLoginModel: Contract.ILoginModel? = null
    private var loginPresenter:Contract.ILoginPresenter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initView()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onRestart() {
        super.onRestart()
    }

    private fun initView() {
        txt_register_now.setOnClickListener(this)
        btn_login.setOnClickListener(this)
    }

    override fun onClick(view: View?) {
        when(view!!.id) {
            R.id.txt_register_now -> {
                if(ClickUtils.isDoubleClick(txt_register_now)) {
                    return
                }
                startActivity(Intent(this@LoginActivity,RegisterActivity::class.java))
            }
            R.id.btn_login -> {
                if(ClickUtils.isDoubleClick(btn_login)) {
                    return
                }
                val username =  txt_user_name.text.toString().trim()
                val password =  txt_password.text.toString().trim()
                if(TextUtils.isEmpty(username)) {
                    CommonToast.show(this@LoginActivity,"用户名不能为空")
                    return
                }
                if(TextUtils.isEmpty(password)) {
                    CommonToast.show(this@LoginActivity,"密码不能为空")
                    return
                }
                onLogin(username,password)
            }
        }
    }

    /**
     * 登录
     */
    private fun onLogin(username:String,password: String) {
        val loginUrl = UrlConstants.BASE_URL + UrlConstants.URL_USER_LOGIN
        val mapHeader = HashMap<String,String>()
        val mapData = HashMap<String,String>()
        mapData["username"] = username
        mapData["password"] = password

        iLoginModel = LoginModel(this@LoginActivity)
        val reference = WeakReference(this)
        loginPresenter = LoginPresenter(iLoginModel, reference.get())
        if(loginPresenter != null) {
            (loginPresenter as LoginPresenter).request(loginUrl,mapHeader,mapData)
        }
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
        return super.onKeyDown(keyCode, event)
    }


    override fun loginSuccess(loginBean: LoginBean?) {
        CommonToast.show(this@LoginActivity,"登录成功")
        SharedPreferenceManager.getInstance(this@LoginActivity).save(SharePreferenceConstants.KEY_ISLOGIN,true)
        SharedPreferenceManager.getInstance(this@LoginActivity).save(SharePreferenceConstants.KEY_USERNAME,loginBean!!.username)
        startActivity(Intent(this@LoginActivity,MainActivity::class.java))
        finish()
    }

    override fun loginFailed(message: String?) {
        if (message != null) {
            CommonToast.show(this@LoginActivity,message)
        }
    }
}